﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;


namespace Catsland.Core
{
	public class ColliderList : RepeatableList<Collider>
	{
		List<Collider> m_removeList;
		
		// TODO: make it a quad-tree

		// skip iCollider and the boundingBox belongs to iCollider, however, 
		// boundingBox may be different from iCollider.m_rootBoundingBox, because boundingBox maybe different offset
		public Collider JudgeCollide(CatsBoundingBox boundingBox, Vector2 XYOffset, float ZOffset = 0.0f, Collider iCollider = null)
		{
			if (m_list == null)
			{
				return null;
			}
			foreach (Collider collider in m_list)
			{
				if (collider == iCollider)
				{
					continue;
				}
				bool result = collider.JudgeCollide(boundingBox, XYOffset, ZOffset);
				if (result == true)
				{
					if (collider.ColliderTypeAttribute == Collider.ColliderType.Collider)
					{
						return collider;
					}
				}
			}
			return null;
		}

		public Collider[] JudgeCollides(CatsBoundingBox boundingBox, Vector2 XYOffset, float ZOffset = 0.0f, Collider iCollider = null)
		{
			
			if (m_list == null)
			{
				return null;
			}
			List<Collider> feedback = new List<Collider>();
			foreach (Collider collider in m_list)
			{
				if (collider == iCollider)
				{
					continue;
				}
				bool result = collider.JudgeCollide(boundingBox, XYOffset, ZOffset);
				if (result == true)
				{
					feedback.Add(collider);
				}
			}
			return feedback.ToArray();
		}

		public override void RemoveItem(Collider item)
		{
			if (m_removeList == null)
			{
				m_removeList = new List<Collider>();
			}
			m_removeList.Add(item);
		}

		public void UpdateRemove()
		{
			if (m_removeList != null)
			{
				foreach (Collider collider in m_removeList)
				{
					m_list.Remove(collider);
				}
				m_removeList.Clear();
			}
		}
	
	}
}
